package com.cupk.inspection.service;

import com.cupk.inspection.model.Device;
import com.cupk.inspection.repository.DeviceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DeviceService {

    @Autowired
    private DeviceRepository deviceRepository;

    // 获取所有设备
    public List<Device> getAllDevices() {
        return deviceRepository.findAll();
    }

    // 根据 ID 获取设备
    public Optional<Device> getDeviceById(Long id) {
        return deviceRepository.findById(id);
    }

    // 创建设备
    public Device createDevice(Device device) {
        device.setCreatedAt(java.time.LocalDate.now());
        return deviceRepository.save(device);
    }

    // 更新设备
    public Device updateDevice(Long id, Device deviceDetails) {
        Optional<Device> device = deviceRepository.findById(id);
        if (device.isPresent()) {
            Device updatedDevice = device.get();
            updatedDevice.setName(deviceDetails.getName());
            updatedDevice.setLocation(deviceDetails.getLocation());
            updatedDevice.setDescription(deviceDetails.getDescription());
            updatedDevice.setCreatedAt(deviceDetails.getCreatedAt());
            return deviceRepository.save(updatedDevice);
        }
        return null;
    }

    // 删除设备
    public void deleteDevice(Long id) {
        deviceRepository.deleteById(id);
    }
}